package demo.student;

import java.util.ArrayList;

public class RandomStringChooser {
	// 使用ArrayList类，是为了处理数组更方便
	protected ArrayList<String> wordlist;

	public RandomStringChooser(String[] wordArray) {
		wordlist = new ArrayList<String>();
		for (int i = 0; i < wordArray.length; i++) {
			wordlist.add(wordArray[i]);
		}
	}

	public String getNext() {
		if (this.wordlist.size() == 0) {
			return "NONE";
		}

		// Math.random() 返回一个随机数，范围是[0.0, 1.0)
		// (int) 取整数部分, 1.0, 1.5, 1.9 取整都是 1
		int randomIndex = (int) (Math.random() * this.wordlist.size());
		return this.wordlist.remove(randomIndex);
	}

	public static void main(String[] args) {
		String[] wordArray = { "apple", "banana", "cherry", "date", "elderberry", "fig" };
		RandomStringChooser rsc = new RandomStringChooser(wordArray);
		for (int i = 0; i < 10; i++) {
			System.out.println(rsc.getNext());
		}

	}

}
